package org.gwu.seuss.client.commandline;

import java.io.File;

import org.gwu.seuss.ui.commandline.BaseCommandLineArgs;
import org.gwu.seuss.ui.validator.RequestValidator;

public class ClientValidator extends RequestValidator {

	@Override
	public <T extends BaseCommandLineArgs> boolean isValid(T cli) {
		CommandLineArgs cla = (CommandLineArgs) cli;

		if (tooManyArguments(cla)) {
			errorMessage = "too many arguments provided";
			return false;
		}

		if (cla.getUpload() != null) {
			return isValidFile(cla.getUpload());
		}

		if (cla.getUploadId() != null) {
			if (cla.getUpload() == null) {
				errorMessage = "Uploading existing file requires both a file id (-ui) and existing file (-u)";
				return false;
			}
		}
		if (cla.getShareFileId() != null || cla.getShareUsername() != null
				|| cla.getSharePermissions() != null) {
			return isValidShare(cla);
		}

		if (cla.getNewEmail() != null) {
			return isValidLogin(cla.getNewEmail(),
					"new login name has invalid format");
		}

		if (cla.isUpdatePassword()) {
			return isValidPassword(cla.retrievePassword());
		}

		return true;
	}

	private boolean isValidShare(CommandLineArgs cla) {
		if ((cla.getShareFileId() != null && (cla.getSharePermissions() == null || cla
				.getShareUsername() == null))
				|| (cla.getSharePermissions() != null && (cla.getShareFileId() == null || cla
						.getShareUsername() == null))
				|| (cla.getShareUsername() != null && (cla.getShareFileId() == null || cla
						.getSharePermissions() == null))) {
			errorMessage = "Sharing a file requires a shared file (-s), a username to share with (-su), "
					+ "and a permission (-sp) that must be the value upload, download, or remove";
			return false;
		}
		String permission = cla.getSharePermissions().toLowerCase();
		if (!(permission.equals("upload") || permission.equals("download") || permission
				.equals("remove"))) {
			errorMessage = "Sharing permissions must be upload, download, or remove";
			return false;
		}
		return true;
	}

	private boolean isValidFile(String upload) {
		if (!new File(upload).exists()) {
			errorMessage = "File does not exist on host system";
			return false;
		}
		return true;
	}

	private boolean tooManyArguments(CommandLineArgs cla) {

		if (cla.getDeleteFile() != null
				&& (cla.isFileList() || cla.getDownload() != null
						|| cla.isUpdatePassword() || cla.getNewEmail() != null
						|| cla.getUpload() != null || cla.getUploadId() != null
						|| cla.getShareFileId() != null || cla
						.getShareUsername() != null)) {
			return true;
		} else if (cla.getDownload() != null
				&& (cla.isFileList() || cla.getDeleteFile() != null
						|| cla.isUpdatePassword() || cla.getNewEmail() != null
						|| cla.getUpload() != null || cla.getUploadId() != null
						|| cla.getShareFileId() != null || cla
						.getShareUsername() != null)) {
			return true;
		} else if (cla.isUpdatePassword()
				&& (cla.isFileList() || cla.getDeleteFile() != null
						|| cla.getDownload() != null
						|| cla.getNewEmail() != null || cla.getUpload() != null
						|| cla.getUploadId() != null
						|| cla.getShareFileId() != null || cla
						.getShareUsername() != null)) {
			return true;
		} else if (cla.getNewEmail() != null
				&& (cla.isFileList() || cla.getDeleteFile() != null
						|| cla.getDownload() != null || cla.isUpdatePassword()
						|| cla.getUpload() != null || cla.getUploadId() != null
						|| cla.getShareFileId() != null || cla
						.getShareUsername() != null)) {
			return true;
		} else if (cla.getUpload() != null
				&& (cla.isFileList() || cla.getDeleteFile() != null
						|| cla.getDownload() != null || cla.isUpdatePassword()
						|| cla.getNewEmail() != null
						|| cla.getShareFileId() != null || cla
						.getShareUsername() != null)) {
			return true;
		} else if (cla.isFileList()
				&& (cla.getUpload() != null || cla.getDeleteFile() != null
						|| cla.getDownload() != null || cla.isUpdatePassword()
						|| cla.getNewEmail() != null
						|| cla.getUploadId() != null
						|| cla.getShareFileId() != null || cla
						.getShareUsername() != null)) {
			return true;
		} else if (cla.getShareUsername() != null
				&& (cla.getUpload() != null || cla.getDeleteFile() != null
						|| cla.getDownload() != null || cla.isUpdatePassword()
						|| cla.getNewEmail() != null
						|| cla.getUploadId() != null || cla.isFileList())) {
			return true;
		} else if (cla.getShareFileId() != null
				&& (cla.getUpload() != null || cla.getDeleteFile() != null
						|| cla.getDownload() != null || cla.isUpdatePassword()
						|| cla.getNewEmail() != null
						|| cla.getUploadId() != null || cla.isFileList())) {
			return true;
		} else {
			return false;
		}
	}
}
